package com.cloud.api.config;

import com.joey.common.context.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;

/**
 * @author: Joey
 * @Description:
 * @date:2024/6/23 11:29
 */
public class DefaultFeignConfig {

    /**
     * 返回日志级别
     * @return
     */
    @Bean
    public Logger.Level feignLogLevel(){
        return Logger.Level.FULL;
    }

    @Bean
    public RequestInterceptor userInfoRequestInterceptor(){
        return template -> {
            // 获取登录用户
            Long userId = UserContext.getCurrentId();
            if(userId == null) {
                // 如果为空则直接跳过
                return;
            }
//            System.out.println("openFeign转发中...");
            // 如果不为空则放入请求头中，传递给下游微服务
            template.header("user-info", userId.toString());
        };
    }
}
